package com.nd.myapplication;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import java.lang.reflect.Field;



public class MainActivity extends AppCompatActivity {
    View floatView;
    WindowManager mWindowManager;
    LayoutParams wmParams;
    int sW;
    int sH;
    LayoutParams eatParams;
    boolean hasAddEat = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        //activity layout中的乱七八糟的按键等，是为了测试悬浮框是否会影响到应用交互功能
        setContentView(R.layout.activity_main);


    }
    /*
    * 获取系统的StatusBar高度
    * */
    private int getStatusBarHeight() {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            return getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            Log.d("test", "get status bar height fail");
            e1.printStackTrace();
            return 75;
        }
    }
    /*
    *创建悬浮窗
    * */
    private void createFloatView() {
        mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        sW = mWindowManager.getDefaultDisplay().getWidth();
        sH = mWindowManager.getDefaultDisplay().getHeight();


        initWindowParams();//
        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        floatView = inflater.inflate(R.layout.vo_view, null);
        mWindowManager.addView(floatView, wmParams);
        View bo = floatView.findViewById(R.id.bobobo);
        bo.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:

                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return false;
            }
        });

    }

    private void initWindowParams() {
        wmParams = new LayoutParams();
        wmParams.type = LayoutParams.TYPE_SYSTEM_ERROR;//TYPE_SYSTEM_ALERT;//
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN|WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.alpha= 0f;  //设置为透明
       // wmParams.width = 30;
        int height = getStatusBarHeight();  //悬浮窗高度与statusbar高度相同
        wmParams.height = height;

    }
    @Override
    protected void onResume()
    {

        super.onResume();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        createFloatView();

    }
    @Override
    protected void onPause() {
        super.onPause();
        //在onPause时必须要移除悬浮框，否则会影响系统的其他应用，当然关闭的时机可以自己选择
        mWindowManager.removeView(floatView);
    }
}
